let customerListModule = (function () {
	let $selectBox = $('.selectBox'),
		$searchInp = $('.searchInp'),
		$tableBox = $('.tableBox'),
		$tbody = $tableBox.find('tbody'),
		$pageBox = $('.pageBox');

	let lx = "my",
		limit = 10,
		page = 1,
		total = 0,
		totalPage = 1;

	// 从服务器端获取数完成数据绑定
	async function bindHTML() {
		let result = await axios.get("/customer/list", {
			params: {
				lx,
				type: $selectBox.val(),
				search: $searchInp.val().trim(),
				limit,
				page
			}
		});

		$tbody.html("");
		$pageBox.html("");
		if (result.code != 0) return;

		// 存储总数和总页数
		total = parseInt(result.total);
		totalPage = parseInt(result.totalPage);

		// 绑定表格的数据
		result = result.data;
		let str = ``;
		result.forEach(item => {
			let {
				id,
				name,
				sex,
				email,
				phone,
				QQ,
				weixin,
				type,
				address,
				userName
			} = item;
			str += `<tr>
				<td class="w8">${name}</td>
				<td class="w5">${sex==0?'男':'女'}</td>
				<td class="w10">${email}</td>
				<td class="w10">${phone}</td>
				<td class="w10">${weixin}</td>
				<td class="w10">${QQ}</td>
				<td class="w5">${type}</td>
				<td class="w8">${userName}</td>
				<td class="w20">${address}</td>
				<td class="w14" customerId="${id}">
					<a href="javascript:;">编辑</a>
					<a href="javascript:;">删除</a>
					<a href="visit.html?id=${id}">回访记录</a>
				</td>
			</tr>`;
		});
		$tbody.html(str);

		// 分页数据的绑定
		if (totalPage > 1) {
			str = ``;
			page > 1 ? str += `<a href="javascript:;">上一页</a>` : null;
			str += `<ul class="pageNum">`;
			for (let i = 1; i <= totalPage; i++) {
				str += `<li class="${i==page?'active':''}">${i}</li>`;
			}
			str += `</ul>`;
			page < totalPage ? str += `<a href="javascript:;">下一页</a>` : null;
			$pageBox.html(str);
		}
	}

	// 基于事件委托实现需要处理的事情
	function delegate() {
		$tbody.click(async ev => {
			let target = ev.target,
				$target = $(target),
				TAG = target.tagName,
				TEXT = target.innerHTML.trim();
			if (TAG === 'A') {
				let customerId = $target.parent().attr('customerId');
				if (TEXT === "编辑") {
					// 跳转到新增页面：为了区分是修改还是新增，我们需要问号传递一个标识过去，并且告知页面我们需要修改的是谁 useradd.html?id=xxx
					window.location.href = `customeradd.html?id=${customerId}`;
					return;
				}

				if (TEXT === "删除") {
					// 防止误操作，我们在删除前做一个提示
					let flag = confirm(`小主，您确定要删除编号为 [ ${customerId} ] 的信息吗？一但删除可就没了哈~~`);
					if (!flag) return;
					let result = await axios.get('/customer/delete', {
						params: {
							customerId
						}
					});
					if (result.code == 0) {
						alert('小主，您很棒，奴家已经成功为您删除了本条数据~~');
						// 在页面中移除本数据
						$target.parent().parent().remove();
						$checkList = $tbody.find('input[type="checkbox"]');
						return;
					}
					alert('小主，当前网络繁忙，请您刷新后重试~~');
					return;
				}

				// if (TEXT === "回访记录") {
					
					// window.location.href = `customeradd.html?id=${customerId}`;
					// return;
				// }
			}
		});
	}

	// 操作处理
	function handle() {
		$selectBox.change(bindHTML);

		$searchInp.keydown(ev => {
			if (ev.keyCode === 13) {
				bindHTML();
			}
		});

		$pageBox.click(ev => {
			let target = ev.target,
				TAG = target.tagName,
				TEXT = target.innerHTML,
				temp = page;
			if (TAG === "A") {
				if (TEXT === "上一页") {
					temp--;
				}
				if (TEXT === "下一页") {
					temp++;
				}
			}
			if (TAG === "LI") {
				temp = parseInt(TEXT);
			}
			temp !== page ? (page = temp, bindHTML()) : null;
		});
	}

	return {
		init() {
			// 获取传递进来的类型
			let params = window.location.href.queryURLParams();
			params.lx ? lx = params.lx : null;

			bindHTML();
			handle();
			delegate()
		}
	}
})();
customerListModule.init();